package com.senseuni.da.ui.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import com.elvishew.xlog.XLog;
import com.senseuni.common.dialog.CBDialogBuilder;
import com.senseuni.common.dialog.OnDialogResultListener;
import com.senseuni.da.R;
import com.senseuni.da.utils.MmkvUtil;
import com.senseuni.da.utils.SpUtil;

import androidx.appcompat.widget.AppCompatSeekBar;

public class DurationSettingDialog extends CBDialogBuilder {

    private TextView mDurationView;
    private AppCompatSeekBar mSeekBarView;

    private long mDuration = 30;

    public DurationSettingDialog(Context context) {
        super(context, CBDialogBuilder.DIALOG_STYLE_NORMAL, (float) 0.5);
        initView(context);
    }

    private void updateDisplayedTime(long progress) {
        long timeInSeconds = getTimeInSeconds(progress);
        XLog.d("progress:" + progress + ",second:" + timeInSeconds);
        String formattedTime = formatTime(timeInSeconds);
        mDurationView.setText(formattedTime);
    }

    private long getTimeInSeconds(long progress) {
        //23
        //6
        //30
        //3
        //3
        //30
        if (progress <= 30) {
            return progress; // 1-1800 秒
        } else if (progress <= 33) { // 1800秒 + 420个进度 * 30秒 = 3600秒 (1小时)
            return 30 + (progress - 30) * 10; // 每个进度代表10秒
        } else if (progress <= 33 + 6) { // 4小时到12小时
            return 60 + (progress - 33) * 10; // 每格1分钟
        } else if (progress <= 33 + 6 + 58) {
            return 120 + (progress - (33 + 6)) * 60;
        } else if (progress <= 33 + 6 + 58 + 6) {
            return 3600 + (progress - (33 + 6 + 58)) * 600;
        } else {
            return 7200 + (progress - (33 + 6 + 58 + 6)) * 3600; // 每格10分钟
        }
    }

    private String formatTime(long seconds) {
        int hours = (int) (seconds / 3600);
        int minutes = (int) ((seconds % 3600) / 60);
        int secs = (int) (seconds % 60);
        if (hours > 1) {
            return String.format("%dh", hours);
        }
        if (hours == 1) {
            return String.format("%dh%dm", hours, minutes);
        }
        if (minutes > 1) {
            return String.format("%dm", minutes);
        }
        if (minutes == 1) {
            return String.format("%dm%ds", minutes, secs);
        }
        return String.format("%ds", secs);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_duration_setting, null);
        mDuration = SpUtil.INSTANCE.getImageDuration();
        mDurationView = view.findViewById(R.id.tv_duration);
        mSeekBarView = view.findViewById(R.id.seekbar);
        mSeekBarView.setMax(126);
        updateDisplayedTime(mDuration);
        mSeekBarView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                updateDisplayedTime(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        setView(view);
        setTitle(context.getString(R.string.duration_set_title));
        setTouchOutSideCancelable(true);
        showCancelButton(true);
        setButtonClickListener(false, (context1, dialog, whichBtn) -> {
            if (whichBtn == OnDialogBtnClickListener.BUTTON_CONFIRM) {
                int value = mSeekBarView.getProgress();
                long timeInSeconds = getTimeInSeconds(value);
                if (mDuration == timeInSeconds) {
                    dialog.dismiss();
                    return;
                }
                mDuration = timeInSeconds;
                SpUtil.INSTANCE.setImageDuration(mDuration);
                if (mListener != null) {
                    mListener.onResult(dialog, mDuration);
                }
            } else if (whichBtn == OnDialogBtnClickListener.BUTTON_CANCEL) {
                dialog.dismiss();
            }
        });
    }

    private OnDialogResultListener<Long> mListener;

    public void setOnResultListener(OnDialogResultListener<Long> listener) {
        mListener = listener;
    }


}
